package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.po.Student;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * Author：汤小洋
 * Date：2024-09-05 14:09
 * Description：<描述>
 */
@RestController
@RequiredArgsConstructor
public class ProducerController {

    private final RabbitTemplate rabbitTemplate;

    @Value("${shop.exchange}")
    private String exchange;

    @Value("${shop.routingKey}")
    private String routingKey;

    @GetMapping("/send")
    public String send(){
        // rabbitTemplate.convertAndSend("exchange1.direct","key1","hello");
        rabbitTemplate.convertAndSend(exchange, routingKey, new Student(1002,"唐伯虎"));
        return "success";
    }

    @GetMapping("/send2")
    public String send2(){
        rabbitTemplate.convertAndSend("exchange2.fanout", null, "welcome");
        return "success";
    }

    @GetMapping("/send3")
    public String send3(){
        // 为消息指定唯一的ID
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("exchange3.direct", "key3", "张三", correlationData);
        return "success";
    }

    @GetMapping("send4")
    public String send4(){
        for(int i=1;i<300;i++) {
            CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
            rabbitTemplate.convertAndSend("normal.exchange", "normal.key", "消息" + i, correlationData);
        }

        return "success";
    }
}
